package cn.ondraw.ragflow.service.impl;

import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.ragflow.pojo.request.ConvertRequest;
import cn.ondraw.ragflow.pojo.request.File2DocumentRmRequest;
import cn.ondraw.ragflow.service.File2DocumentService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
 * 文件转换和删除服务实现类
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class File2DocumentServiceImpl implements File2DocumentService {
    private final PythonApiCaller pythonApiCaller;

    private final String URL_PREFIX = "/api/v1/file2document";

    /**
     * 执行文件转换操作
     *
     * @param request 转换请求对象
     * @return 包含转换结果的响应实体
     */
    @Override
    public ResponseEntity<String> convert(ConvertRequest request) {
        ResponseEntity<String> response = (ResponseEntity<String>) pythonApiCaller.callPostApi(URL_PREFIX + "/convert", request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody() : null, Object.class, true);
    }

    /**
     * 执行文件删除操作
     *
     * @param request 删除请求对象
     * @return 包含删除结果的响应实体
     */
    @Override
    public ResponseEntity<String> rm(File2DocumentRmRequest request) {
        ResponseEntity<String> response = (ResponseEntity<String>) pythonApiCaller.callPostApi(URL_PREFIX + "/rm", request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody() : null, Object.class, true);
    }
}
